package com.example.pageability.slice;

import com.example.pageability.OtherPageAbilty;
import com.example.pageability.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.data.rdb.ValuesBucket;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x0024, "MY_TAG");

    Text text;
    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (requestCode == 0 && resultIntent != null && text != null) {
            text.setText(resultIntent.getStringParam("data"));
        }
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        Button btn = (Button) findComponentById(ResourceTable.Id_route_button);
        btn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DetailAbilitySlice slice = new DetailAbilitySlice();
                Intent i = new Intent();
                i.setParam("title", "传递数据");
//                present(slice, i);
                presentForResult(slice, i, 0);
            }
        });

        Button btn2 = (Button) findComponentById(ResourceTable.Id_route_button2);
        btn2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent i = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAbilityName(OtherPageAbilty.class.getName())
                        .withBundleName("com.example.pageability")
                        .build();
                i.setOperation(operation);
                i.setParam("title", "title data");
//                startAbility(i);
                startAbilityForResult(i,1);
            }
        });

        Button btn3 = (Button) findComponentById(ResourceTable.Id_route_button3);
        btn3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent i = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAbilityName(OtherPageAbilty.class.getName())
                        .withBundleName("com.example.pageability")
                        .withAction("action.other.detail")
                        .build();
                i.setOperation(operation);
                i.setParam("title", "title data");
                startAbilityForResult(i,1);
            }
        });

        Button btn4 = (Button) findComponentById(ResourceTable.Id_route_button4);
        btn4.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent i = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withAbilityName("com.example.pageability.ServiceAbility")
                        .withBundleName("com.example.pageability")
                        .build();
                i.setOperation(operation);
                i.setParam("data", "service data");
                startAbility(i);
            }
        });

        Button btn5 = (Button) findComponentById(ResourceTable.Id_route_button5);
        DataAbilityHelper helper = DataAbilityHelper.creator(this);
        btn5.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ValuesBucket valuesBucket = new ValuesBucket();
                valuesBucket.putString("name", "珲少");
                valuesBucket.putInteger("age", 31);
                Uri uri = Uri.parse("dataability:///com.example.pageability.DataAbility");
                try {
                    helper.insert(uri, valuesBucket);
                } catch (Exception e){
                    HiLog.error(LABEL, e.getMessage());
                };
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
